//
//  CircleProgressView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/24.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "CircleProgressView.h"

#define degreesToRadians(x) (M_PI*(x)/180.0) //把角度转换成PI的方式
#define PROGREESS_WIDTH jisuan(290) //圆直径
#define PROGRESS_LINE_WIDTH jisuan(30) //弧线的宽度

#define Colorafd4ff [UIColor colorWithHexString:@"afd4ff"]
#define Color6fe0fd [UIColor colorWithHexString:@"6fe0fd"]



@implementation CircleProgressView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        _strokeColor = Colorafd4ff;
        
        _trackLayer = [CAShapeLayer layer];
        _trackLayer.frame = self.bounds;
        [self.layer addSublayer:_trackLayer];
        
        _trackLayer.fillColor = [UIColor clearColor].CGColor;
        _trackLayer.strokeColor = _strokeColor.CGColor;
        _trackLayer.opacity = 0.2;
        _trackLayer.lineWidth = PROGRESS_LINE_WIDTH;
        _trackLayer.lineCap = kCALineCapRound;
        
        
        CGPoint point = CGPointMake(PROGREESS_WIDTH / 2.0, PROGREESS_WIDTH / 2.0);
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:point radius:(PROGREESS_WIDTH - PROGRESS_LINE_WIDTH)/2 startAngle:degreesToRadians(-220) endAngle:degreesToRadians(40) clockwise:YES];
        _trackLayer.path = path.CGPath;
        
        _progressLayer = [[CAShapeLayer alloc] init];
        _progressLayer.frame = self.bounds;
        _progressLayer.fillColor = [UIColor clearColor].CGColor;
        _progressLayer.strokeColor = Colorff0000.CGColor;
        _progressLayer.lineCap = kCALineCapRound;
        _progressLayer.lineWidth = PROGRESS_LINE_WIDTH;
        _progressLayer.path = path.CGPath;
        _progressLayer.strokeEnd = 0;
        
      
        CALayer *gradientLayer=[CALayer layer];
        CAGradientLayer *gradientLayer1=[CAGradientLayer layer];
        gradientLayer1.frame=CGRectMake(0, 0, self.bounds.size.width,  self.bounds.size.height);
        [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)Color1f89ff.CGColor,(id)[Color6fe0fd CGColor], nil]];
        
        //注意，要把这行注释掉，不然进度条位置偏移 显示不出来
        
        //[gradientLayer1 setLocations:@[@0.5,@0.9,@1]];
        [gradientLayer1 setStartPoint:CGPointMake(0, 0)];//调整颜色比例主要是调这两个参数（0－1之间）
        [gradientLayer1 setEndPoint:CGPointMake(1 , 0)];//调整颜色比例主要是调这两个参数（0－1之间）
        [gradientLayer addSublayer:gradientLayer1];
        
        
        //如果只需要两种颜色渐变，把下边的gradientLayer2 相关代码去掉就行
//        CAGradientLayer *gradientLayer2=[CAGradientLayer layer];
//        gradientLayer2.frame=CGRectMake(self.bounds.size.width/2, 0,self.bounds.size.width/2 , self.bounds.size.height);
//        [gradientLayer2 setColors:[NSArray arrayWithObjects:(id)[UIColor yellowColor].CGColor,(id)[UIColor blueColor].CGColor, nil]];
//
//        //注意，要把这行注释掉，不然进度条位置偏移 显示不出来
//
//        //[gradientLayer2 setLocations:@[@0.1,@0.5,@1]];
//        [gradientLayer2 setStartPoint:CGPointMake(0.5,0.2)];//调整颜色比例主要是调这两个参数（0－1之间）
//        [gradientLayer2 setEndPoint:CGPointMake(0.5, 0.5)];//调整颜色比例主要是调这两个参数（0－1之间）
//
//        [gradientLayer addSublayer:gradientLayer2];
        
        
        [gradientLayer setMask:_progressLayer];
        [self.layer addSublayer:gradientLayer];
        
    }
    
    return self;
}

//设置进度条的百分比
-(void)setPercent:(CGFloat)percent animated:(BOOL)animated{

    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    _progressLayer.strokeEnd = percent/100.0;
    
    [CATransaction commit];
 
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
